package com.example.metesting;

import java.util.Calendar;

import com.example.metesting.TimePickerFragment.OnTimePickedListener;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.os.Bundle;
import android.widget.DatePicker;

public class DatePickerFragment extends DialogFragment implements
		DatePickerDialog.OnDateSetListener {

	OnDatePickedListener mCallback;
	Integer pickerId = null;

	public static DatePickerFragment newInstance(int id, int year, int month,
			int day) {

		Bundle args = new Bundle();
		args.putInt("picker_id", id);
		args.putInt("year", year);
		args.putInt("month", month);
		args.putInt("day", day);
		DatePickerFragment fragment = new DatePickerFragment();
		fragment.setArguments(args);
		return fragment;
	}

	public interface OnDatePickedListener {
		public void onDatePicked(int pickerId, int year, int month, int day);
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);

		// This makes sure that the container activity has implemented
		// the callback interface. If not, it throws an exception
		try {
			mCallback = (OnDatePickedListener) activity;
		} catch (ClassCastException e) {
			throw new ClassCastException(activity.toString()
					+ " must implement OnDatePickedListener.");
		}
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		mCallback = (OnDatePickedListener) getActivity();

		Bundle args = getArguments();

		// Use the current time as the default values for the picker
		final Calendar c = Calendar.getInstance();
		int year = args.getInt("year", c.get(Calendar.YEAR));
		int month = args.getInt("month", c.get(Calendar.MONTH));
		int day = args.getInt("day", c.get(Calendar.DAY_OF_MONTH));
		pickerId = getArguments().getInt("picker_id");

		// Create a new instance of DatePickerDialog and return it
		return new DatePickerDialog(getActivity(), this, year, month, day);
	}

	public void onDateSet(DatePicker view, int year, int month, int day) {
		// Do something with the date chosen by the user
		if (mCallback != null) {

			mCallback.onDatePicked(pickerId, year, month, day);
		}
	}
}